package com.ruoyi.web.controller.system;

import java.util.List;
import java.util.Map;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.framework.web.service.TokenService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.TourismCharacteristic;
import com.ruoyi.system.service.ITourismCharacteristicService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 特色商品Controller
 *
 * @author gqd
 * @date 2020-12-25
 */
@RestController
@RequestMapping("/system/characteristic")
public class TourismCharacteristicController extends BaseController
{
    @Autowired
    private ITourismCharacteristicService tourismCharacteristicService;
    @Resource
    private TokenService tokenService;

    /**
     * 查询特色商品列表
     */
    @PreAuthorize("@ss.hasPermi('system:characteristic:list')")
    @GetMapping("/list")
    public TableDataInfo list(TourismCharacteristic tourismCharacteristic)
    {
        startPage();
        List<TourismCharacteristic> list = tourismCharacteristicService.selectTourismCharacteristicList(tourismCharacteristic);
        return getDataTable(list);
    }

    /**
     * 导出特色商品列表
     */
    @PreAuthorize("@ss.hasPermi('system:characteristic:export')")
    @Log(title = "特色商品", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TourismCharacteristic tourismCharacteristic)
    {
        List<TourismCharacteristic> list = tourismCharacteristicService.selectTourismCharacteristicList(tourismCharacteristic);
        ExcelUtil<TourismCharacteristic> util = new ExcelUtil<TourismCharacteristic>(TourismCharacteristic.class);
        return util.exportExcel(list, "characteristic");
    }

    /**
     * 获取特色商品详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:characteristic:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return AjaxResult.success(tourismCharacteristicService.selectTourismCharacteristicById(id));
    }

    /**
     * 新增特色商品
     */
    @PreAuthorize("@ss.hasPermi('system:characteristic:add')")
    @Log(title = "特色商品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TourismCharacteristic tourismCharacteristic)
    {
        return toAjax(tourismCharacteristicService.insertTourismCharacteristic(tourismCharacteristic));
    }

    /**
     * 修改特色商品
     */
    @PreAuthorize("@ss.hasPermi('system:characteristic:edit')")
    @Log(title = "特色商品", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TourismCharacteristic tourismCharacteristic)
    {
        return toAjax(tourismCharacteristicService.updateTourismCharacteristic(tourismCharacteristic));
    }

    /**
     * 删除特色商品
     */
    @PreAuthorize("@ss.hasPermi('system:characteristic:remove')")
    @Log(title = "特色商品", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(tourismCharacteristicService.deleteTourismCharacteristicByIds(ids));
    }

    /**
     * 查询所有的商品
     * @param map
     * @return
     */
    @PostMapping("/selectAll")
    public AjaxResult selectAll(@RequestBody Map map){
        String characteristicName = (String) map.get("characteristicName");
        return tourismCharacteristicService.selectAll(characteristicName);
    }

    /**
     * 查询农副产品的信息
     * @param map
     * @return
     */
    @PostMapping("/selectByStatusOne")
    public AjaxResult selectByStatusOne(@RequestBody Map map){
        String characteristicName = (String) map.get("characteristicName");
        return tourismCharacteristicService.selectByStatusOne(characteristicName);
    }

    /**
     * 根据商品id查询商品的详细的信息
     * @param map
     * @return
     */
    @PostMapping("/selectById")
    public AjaxResult selectById(@RequestBody Map map, HttpServletRequest request){
        LoginUser loginUser = tokenService.getLoginUser(request);
        if(loginUser != null){
            Integer userId = loginUser.getUserId();
            Integer id = (Integer) map.get("id");
            return tourismCharacteristicService.selectById(id,userId);
        }else {
            Integer userId = null ;
            Integer id = (Integer) map.get("id");
            return tourismCharacteristicService.selectById(id,userId);
        }

    }

    /**
     * 查询根雕
     * @param map
     * @return
     */
    @PostMapping("/selectByStatusTwo")
    public AjaxResult selectByStatusTwo(@RequestBody Map map){
        String characteristicName = (String) map.get("characteristicName");
        return tourismCharacteristicService.selectByStatusTwo(characteristicName);
    }

    /**
     * 查询非遗产品
     * @param map
     * @return
     */
    @PostMapping("/selectByStatusThree")
    public AjaxResult selectByStatusThree(@RequestBody Map map){
        String characteristicName = (String) map.get("characteristicName");
        return tourismCharacteristicService.selectByStatusThree(characteristicName);
    }

    @PostMapping("/selectCharacteristic")
    public AjaxResult selectCharacteristic(@RequestBody Map map){
        String characteristicName = (String) map.get("characteristicName");
        Integer status = (Integer) map.get("status");
        return tourismCharacteristicService.selectCharacteristic(characteristicName,status);
    }
}
